package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.MyNotice;
import com.hl.sfzy.service.MyNoticeService;
import com.hl.sfzy.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/myNotice")
public class MyNoticeController {
	@Autowired
	private MyNoticeService myNoticeService;
	@PostMapping("/add")
	public R add(@RequestBody MyNotice myNotice) {
		int add = myNoticeService.add(myNotice);
		if (add != 0) {
			return R.ok(add);
		} else {
			return R.fail("添加失败");
		}
	}

	@PutMapping("/edit")
	public R edit(@RequestBody MyNotice myNotice) {
		int edit = myNoticeService.edit(myNotice);
		if (edit != 0) {
			return R.ok(edit);
		} else {
			return R.fail("修改失败");
		}
	}

	@DeleteMapping("/remove")
	public R remove(Integer myNoticeId) {
		int remove = myNoticeService.remove(myNoticeId);
		if (remove != 0) {
			return R.ok(remove);
		} else {
			return R.fail("删除失败");
		}
	}

	@DeleteMapping("/removeBatch")
	public R removeBatch(Integer[] myNoticeIds) {
		int removeBatch = myNoticeService.removeBatch(myNoticeIds);
		if (removeBatch != 0) {
			return R.ok(removeBatch);
		} else {
			return R.fail("批量删除失败");
		}
	}

	@GetMapping("/find")
	public R find() {
		List<MyNotice> find = myNoticeService.find();
		if (find != null) {
			return R.ok(find);
		} else {
			return R.fail("查询失败");
		}
	}

	@GetMapping("/findById")
	public R findById(Integer myNoticeId) {
		MyNotice findById = myNoticeService.findById(myNoticeId);
		if (findById != null) {
			return R.ok(findById);
		} else {
			return R.fail("查询失败");
		}
	}

//

	@GetMapping("/findByPage")
	public R findByPage(@RequestParam(required = false ,defaultValue = "1")int pageNum,
	                    @RequestParam(required = false,defaultValue = "5") int pageSize,MyNotice myNotice){
		PageInfo<MyNotice> pageInfo = myNoticeService.findByPage(pageNum, pageSize, myNotice);
		return R.ok(pageInfo);
	}



}
